package mq.job;

import common.MyDisReentrantLock;
import lombok.extern.slf4j.Slf4j;
import mq.domain.MqMessageCumulate;
import mq.service.IMqMessageCumulateService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.annotation.Resource;

/**
 * @description: 堆积消息补偿处理
 * @author: zk
 * @since: 2022-08-21 09:26:41
 */
@Slf4j
public class CumulateMessageHandleJob extends QuartzJobBean {
    private final String MQ_CUMULATE_MESSAGE_HANDLE_LOCK = "MQ_CUMULATE_MESSAGE_HANDLE_LOCK";
    @Resource
    private MyDisReentrantLock myDisReentrantLock;
    @Resource
    private IMqMessageCumulateService mqMessageCumulateService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("堆积消息补偿处理定时任务 begin");
        boolean lock = myDisReentrantLock.lock(CumulateMessageHandleJob.class, MQ_CUMULATE_MESSAGE_HANDLE_LOCK, 7200);
        if (!lock) {
            log.info("堆积消息补偿处理定时任务,已经有任务在处理 end");
            return;
        }
        try {
            mqMessageCumulateService.cumulateMessageHandle(MqMessageCumulate.BUSI_NO_DEDUCTION);
        } catch (Exception e) {
            log.error("堆积消息补偿处理执行失败", e);
        } finally {
            myDisReentrantLock.unLock(CumulateMessageHandleJob.class, MQ_CUMULATE_MESSAGE_HANDLE_LOCK);
            log.info("堆积消息补偿处理定时任务 end");
        }
    }
}
